package com.demo.autotest.controller;

import com.demo.autotest.common.utils.PageUtils;
import com.demo.autotest.database.domain.Project;
import com.demo.autotest.database.domain.TestFile;
import com.demo.autotest.dto.PageDTO;
import com.demo.autotest.dto.TestFileDTO;
import com.demo.autotest.request.QueryRequest;
import com.demo.autotest.service.TestFileService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

@RestController // 定义一个controller类
@RequestMapping("/autotest/testFile")
public class TestFileController {

    @Resource
    private TestFileService testFileService;

    @PostMapping(value = "/upload", consumes = {"multipart/form-data"})
    public void upload(@RequestParam String name, @RequestParam String projectId, @RequestParam String description,
                       @RequestParam MultipartFile file, HttpServletRequest request){
        String user = request.getSession().getAttribute("userId").toString();
        TestFile testFile = new TestFile();
        testFile.setName(name);
        testFile.setProjectId(projectId);
        testFile.setDescription(description);
        testFile.setCreateUser(user);
        testFileService.upload(file, testFile);
    }

    @PostMapping("/delete/{fileId}")
    public void delete(@PathVariable String fileId){
        testFileService.delete(fileId);
    }

    @PostMapping("/list/{pageNum}/{pageSize}")
    public PageDTO<List<TestFileDTO>> getTestFileList(@PathVariable Integer pageNum, @PathVariable Integer pageSize,
                                                     @RequestBody QueryRequest queryRequest){
        Page<Object> page = PageHelper.startPage(pageNum, pageSize, true);
        return PageUtils.setPageInfo(page, testFileService.getTestFileList(queryRequest));
    }
}
